Max area of island

Time: O(MxN); Space: O(MN); easy*

Given a non-empty 2D array grid of 0’s and 1’s, an island is a group of 1’s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)

Example 1:

Input: grid =

[
 [0,0,1,0,0,0,0,1,0,0,0,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,1,1,0,1,0,0,0,0,0,0,0,0],
 [0,1,0,0,1,1,0,0,1,0,1,0,0],
 [0,1,0,0,1,1,0,0,1,1,1,0,0],
 [0,0,0,0,0,0,0,0,0,0,1,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,0,0,0,0,0,0,1,1,0,0,0,0]

]

Output: 6

Explanation:

  • Note the answer is not 11, because the island must be connected 4-directionally.

Example 2:

Input: grid = [[0,0,0,0,0,0,0,0]]

Output: 0

Note:

  • The length of each dimension in the given grid does not exceed 50.

1. Depth-First Search (Recursive) [O(RxC), O(RxC)]

Intuition and Algorithm

We want to know the area of each connected shape in the grid, then take the maximum of these.

If we are on a land square and explore every square connected to it 4-directionally (and recursively squares connected to those squares, and so on), then the total number of squares explored will be the area of that connected shape.

To ensure we don’t count squares in a shape more than once, let’s use seen to keep track of squares we haven’t visited before. It will also prevent us from counting the same shape more than once.

[1]:
class Solution1(object):
    """
    Time: O(RxC), where R is the number of rows in the given grid, and C is the number of columns.
          We visit every square once.
    Space: O(RxC), the space used by seen to keep track of visited squares,
           and the space used by the call stack during our recursion.
    """
    def maxAreaOfIsland(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        seen = set()
        def area(r, c):
            if not (0 <= r < len(grid) and 0 <= c < len(grid[0])
                    and (r, c) not in seen and grid[r][c]):
                return 0
            seen.add((r, c))
            return (1 + area(r+1, c) + area(r-1, c) +
                    area(r, c-1) + area(r, c+1))

        return max(area(r, c)
                   for r in range(len(grid))
                   for c in range(len(grid[0])))
[2]:
s = Solution1()
grid = [
     [0,0,1,0,0,0,0,1,0,0,0,0,0],
     [0,0,0,0,0,0,0,1,1,1,0,0,0],
     [0,1,1,0,1,0,0,0,0,0,0,0,0],
     [0,1,0,0,1,1,0,0,1,0,1,0,0],
     [0,1,0,0,1,1,0,0,1,1,1,0,0],
     [0,0,0,0,0,0,0,0,0,0,1,0,0],
     [0,0,0,0,0,0,0,1,1,1,0,0,0],
     [0,0,0,0,0,0,0,1,1,0,0,0,0]
   ]
assert s.maxAreaOfIsland(grid) == 6
grid = [[0,0,0,0,0,0,0,0]]
assert s.maxAreaOfIsland(grid) == 0

2. Depth-First Search (Iterative) [O(RxC), O(RxC)]

Intuition and Algorithm

We can try the same approach using a stack based, (or “iterative”) depth-first search.

Here, seen will represent squares that have either been visited or are added to our list of squares to visit (stack). For every starting land square that hasn’t been visited, we will explore 4-directionally around it, adding land squares that haven’t been added to seen to our stack.

On the side, we’ll keep a count shape of the total number of squares seen during the exploration of this shape. We’ll want the running max of these counts.

[3]:
class Solution2(object):
    """
    Time: O(RxC), where R is the number of rows in the given grid, and C is the number of columns.
          We visit every square once.
    Space: O(RxC), the space used by seen to keep track of visited squares, and the space used by stack.
    """
    def maxAreaOfIsland(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        seen = set()
        ans = 0
        for r0, row in enumerate(grid):
            for c0, val in enumerate(row):
                if val and (r0, c0) not in seen:
                    shape = 0
                    stack = [(r0, c0)]
                    seen.add((r0, c0))
                    while stack:
                        r, c = stack.pop()
                        shape += 1
                        for nr, nc in ((r-1, c), (r+1, c), (r, c-1), (r, c+1)):
                            if (0 <= nr < len(grid) and 0 <= nc < len(grid[0])
                                    and grid[nr][nc] and (nr, nc) not in seen):
                                stack.append((nr, nc))
                                seen.add((nr, nc))
                    ans = max(ans, shape)
        return ans
[4]:
s = Solution2()
grid = [
     [0,0,1,0,0,0,0,1,0,0,0,0,0],
     [0,0,0,0,0,0,0,1,1,1,0,0,0],
     [0,1,1,0,1,0,0,0,0,0,0,0,0],
     [0,1,0,0,1,1,0,0,1,0,1,0,0],
     [0,1,0,0,1,1,0,0,1,1,1,0,0],
     [0,0,0,0,0,0,0,0,0,0,1,0,0],
     [0,0,0,0,0,0,0,1,1,1,0,0,0],
     [0,0,0,0,0,0,0,1,1,0,0,0,0]
   ]
assert s.maxAreaOfIsland(grid) == 6
grid = [[0,0,0,0,0,0,0,0]]
assert s.maxAreaOfIsland(grid) == 0

3. Depth-First Search [O(RxC), O(RxC)]

[5]:
class Solution3(object):
    """
    Time: O(RxC)
    Space: O(RxC), the max depth of DFS may be RxC.
    """
    def maxAreaOfIsland(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        directions = [[-1,  0], [ 1,  0], [ 0,  1], [ 0, -1]]

        def dfs(i, j, grid, area):
            if not (0 <= i < len(grid) and \
                    0 <= j < len(grid[0]) and \
                    grid[i][j] > 0):
                return False
            grid[i][j] *= -1
            area[0] += 1
            for d in directions:
                dfs(i+d[0], j+d[1], grid, area)
            return True

        result = 0
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                area = [0]
                if dfs(i, j, grid, area):
                    result = max(result, area[0])
        return result
[6]:
s = Solution3()
grid = [
     [0,0,1,0,0,0,0,1,0,0,0,0,0],
     [0,0,0,0,0,0,0,1,1,1,0,0,0],
     [0,1,1,0,1,0,0,0,0,0,0,0,0],
     [0,1,0,0,1,1,0,0,1,0,1,0,0],
     [0,1,0,0,1,1,0,0,1,1,1,0,0],
     [0,0,0,0,0,0,0,0,0,0,1,0,0],
     [0,0,0,0,0,0,0,1,1,1,0,0,0],
     [0,0,0,0,0,0,0,1,1,0,0,0,0]
   ]
assert s.maxAreaOfIsland(grid) == 6
grid = [[0,0,0,0,0,0,0,0]]
assert s.maxAreaOfIsland(grid) == 0